package com.xgst.land.viewmodel

import android.Manifest
import com.blankj.utilcode.util.PermissionUtils
import com.blankj.utilcode.util.PermissionUtils.SimpleCallback
import com.blankj.utilcode.util.ToastUtils
import com.hjq.http.EasyHttp
import com.hjq.http.listener.OnHttpListener
import com.xgst.land.base.BaseViewModel
import com.xgst.land.bean.LoginBean
import com.xgst.land.http.api.LoginAPI
import com.xgst.land.utils.ProgressUtils
import java.lang.Exception

class LoginViewModel : BaseViewModel() {
    fun login(account:String , psw:String, onResult:(LoginBean)->Unit){
        EasyHttp.post(this)
            .api(LoginAPI(account,psw))
            .request(object : OnHttpListener<LoginBean>{
                override fun onSucceed(result: LoginBean) {
                    onResult(result)
                }

                override fun onFail(e: Exception) {
                    ToastUtils.showShort(e.message)
                    ProgressUtils.dismiss()
                }
            })

    }


    fun checkPermission(onGranted:()->Unit){
        PermissionUtils.permission(Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA)
            .callback(object : SimpleCallback{
                override fun onGranted() {
                    onGranted()
                }

                override fun onDenied() {

                }
            })
            .request()
    }
}
